var createError = require('http-errors');
var express = require('express');
var cors = require('cors')
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
//引入路由器模块
const aboutRouter = require('./routes/about')
const newsRouter = require('./routes/news')
const news2Router =require('./routes/news2')
const busiRouter =require('./routes/busi')
const coopRouter=require('./routes/coop')
const jobRouter=require('./routes/job')
const  userRouter = require('./routes/user')
const productRouter = require('./routes/product')
const  shoppingRouter=require('./routes/shopping')
const orderRouter = require('./routes/order')
//创建服务器
var app = express();


//打印请求日志
app.use(logger('dev'));
//将post传参转对象
app.use(express.json());
app.use(cors())
app.use(express.urlencoded({ extended: true }));
//静态资源托管到文件夹public
app.use(express.static(path.join(__dirname, 'public')));
//注册路由器模块
app.use('/v1/about',aboutRouter)
app.use('/v1/news',newsRouter)
app.use('/v1/news2',news2Router)
app.use('/v1/busi',busiRouter)
app.use('/v1/coop',coopRouter)
app.use('/v1/job',jobRouter)
app.use('/v1/user',userRouter)
app.use('/v1/product',productRouter)
app.use('/v1/shopping',shoppingRouter)
app.use('/v1/order',orderRouter)
//处理404中间件
// catch 404 and forward to error handler
app.use(function(req, res, next) {
	res.send({code:404,msg:'资源不存在'})
});

// error handler
//错误处理中间件
app.use(function(err, req, res, next) {
	console.log(err)
	res.send({code:500,msg:'服务器端错误'})
});

module.exports = app;


